<?php

namespace app\admin\controller;

use Exception;
use think\response\Json;

class User extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 搜索用户
     */
    public function search_user(): Json
    {
        try {
            //接收传递过来的条件参数
            //用户名
            $username = request()->get('username') ?? "";
            //邮箱
            $email    = request()->get('email') ?? "";
            //手机号
            $phone    = request()->get('phone') ?? "";
            //状态
            $status = request()->get('status') ?? "";
            //开始时间
            $startTime = request()->get('startTime') ?? "";
            //结束时间
            $endTime = request()->get('endTime') ?? "";

            //过滤条件
            $filterCriteria = [];
            if (!empty($username)) {
                $filterCriteria['Name'] = $username;
            }
            if (!empty($email)) {
                $filterCriteria['Email'] = $email;
            }
            if (!empty($phone)) {
                $filterCriteria['Phone'] = $phone;
            }
            if ($status != "") {
                $filterCriteria['Status'] = $status;
            }
            if (!empty($startTime) || !empty($endTime)) {
                $filterCriteria['AddTime'] = ['start' => $startTime, 'end' => $endTime];
            }

            //排序条件
            $sorterCriteria = ['ID' => false];

            $dataList = $this->getUserService()->searchUser($this->page, $this->pageSize, $filterCriteria, $sorterCriteria);
            $dataArr = $dataList->getData();
            $dataCount = $dataList->getCount();

            $data = [];
            if (!empty($dataArr)) {
                foreach ($dataArr as $val) {
                    $data[] = [
                        'id' => $val->getId(),
                        'username' => $val->getUsername(),
                        'sex'   => $val->getSex(),
                        'phone' => $val->getPhone(),
                        'email' => $val->getEmail(),
                        'birthday'  => empty($val->getBirthday()) ? "" : date("Y-m-d", $val->getBirthday()),
                        'integral'  => $val->getIntegral(),
                        'status' => $val->getStatus(),
                        'statusRemark' => $val->getStatusRemark(),
                        'vipExpireTime' => empty($val->getVipExpireTime()) ? "" : date("Y-m-d H:i:s", $val->getVipExpireTime()),
                        'addTime' => $val->getAddTime()
                    ];
                }
            }
            $resultData = [
                "total" => $dataCount,
                "records" => $data
            ];
            return $this->requestSuccessReturn($resultData);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加用户
     */
    public function add_user(): Json
    {
        try {
            //用户名
            $username = request()->post('username');
            //密码
            $password = request()->post('password');
            if (empty($username)) {
                throw new Exception("用户名必传");
            }
            if (empty($password)) {
                throw new Exception("密码必传");
            }

            $email = request()->post('email');
            $phone = request()->post('phone');

            $user = new \service\vo\user\User();
            $user->setUsername($username);
            $user->setPassword($password);
            $user->setEmail($email);
            $user->setPhone($phone);

            $id = $this->getUserService()->addUser($user);
            $data = ['id' => $id];
            return $this->requestSuccessReturn($data);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //编辑用户
    public function edit_user(): Json
    {
        try {
            //id
            $id = request()->put('id');
            //密码
            $password = request()->put('password');
            //手机号
            $phone = request()->put('phone');
            //邮箱
            $email = request()->put('email');
            //状态
            $status = request()->put('status');
            //状态描述
            $statusRemark = request()->put('statusRemark');
            if (empty($id)) {
                throw new Exception("id必传");
            }

            $user = new \service\vo\user\User();
            $user->setId($id);
            $user->setPassword("");
            if (!empty($password)) {
                $user->setPassword($password);
            }
            $user->setEmail($email);
            $user->setPhone($phone);
            $user->setStatus($status);
            $user->setStatusRemark($statusRemark);
            $user->setSex(0);
            $user->setBirthday(0);
            $this->getUserService()->editUser($user);
            return $this->requestSuccessReturn([]);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //获取用户
    public function get_user(): Json
    {
        try {
            //id
            $id = request()->get('id');

            if (empty($id)) {
                throw new Exception("id必传");
            }

            $user = $this->getUserService()->getUserById($id);
            $data = [
                'id' => $user->getId(),
                'username' => $user->getUsername(),
                'email' => $user->getEmail(),
                'phone' => $user->getPhone(),
                'birthday'  => empty($user->getBirthday()) ? "" : date("Y-m-d", $user->getBirthday()),
                'integral'  => $user->getIntegral(),
                'status' => $user->getStatus(),
                'statusRemark' => $user->getStatusRemark(),
                'vipExpireTime' => $user->getVipExpireTime(),
                'addTime' => $user->getAddTime(),
            ];
            return $this->requestSuccessReturn($data);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }

    //删除用户
    public function delete_user(): Json
    {
        try {
            //id
            $id = request()->delete('id');

            if (empty($id)) {
                throw new Exception("id必传");
            }

            $result = $this->getUserService()->deleteUser($id);
            if ($result == 0) {
                throw new Exception("删除失败");
            }
            return $this->requestSuccessReturn([]);
        } catch (Exception $e) {
            return $this->requestFailReturn($e->getMessage(), $e->getCode());
        }
    }
}